package com.ruoyi.project.modules.statusProcess.controller;

import java.util.List;

import com.ruoyi.project.modules.table.domain.FieldStatus;
import com.ruoyi.project.modules.table.service.IFieldStatusService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.statusProcess.domain.StatusProcess;
import com.ruoyi.project.modules.statusProcess.service.IStatusProcessService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 状态流程实例Controller
 *
 * @author smallrain
 * @date 2021-11-12
 */
@Controller
@RequestMapping("/statusProcess/statusProcess")
public class StatusProcessController extends BaseController {
    private String prefix = "statusProcess/statusProcess";

    @Autowired
    private IStatusProcessService statusProcessService;

    @RequiresPermissions("statusProcess:statusProcess:view")
    @GetMapping()
    public String statusProcess() {
        return prefix + "/statusProcess";
    }

    /**
     * 查询状态流程实例列表
     */
    @RequiresPermissions("statusProcess:statusProcess:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StatusProcess statusProcess) {
        startPage();
        List<StatusProcess> list = statusProcessService.selectStatusProcessList(statusProcess);
        return getDataTable(list);
    }

    /**
     * 导出状态流程实例列表
     */
    @RequiresPermissions("statusProcess:statusProcess:export")
    @Log(title = "状态流程实例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StatusProcess statusProcess) {
        List<StatusProcess> list = statusProcessService.selectStatusProcessList(statusProcess);
        ExcelUtil<StatusProcess> util = new ExcelUtil<StatusProcess>(StatusProcess.class);
        return util.exportExcel(list, "状态流程实例数据");
    }

    /**
     * 新增状态流程实例
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存状态流程实例
     */
    @RequiresPermissions("statusProcess:statusProcess:add")
    @Log(title = "状态流程实例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StatusProcess statusProcess) {
        return toAjax(statusProcessService.insertStatusProcess(statusProcess));
    }

    /**
     * 修改状态流程实例
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        StatusProcess statusProcess = statusProcessService.selectStatusProcessById(id);
        mmap.put("statusProcess", statusProcess);
        FieldStatus fieldStatus = new FieldStatus();
        fieldStatus.setSceneModel(statusProcess.getSceneModel());
        List<FieldStatus> fieldStatusList = fieldStatusService.selectFieldStatusList(fieldStatus);
        mmap.put("statusList", fieldStatusList);
        return prefix + "/edit";
    }

    /**
     * 修改保存状态流程实例
     */
    @RequiresPermissions("statusProcess:statusProcess:edit")
    @Log(title = "状态流程实例", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StatusProcess statusProcess) {
        return toAjax(statusProcessService.updateStatusProcess(statusProcess));
    }

    /**
     * 删除状态流程实例
     */
    @RequiresPermissions("statusProcess:statusProcess:remove")
    @Log(title = "状态流程实例", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(statusProcessService.deleteStatusProcessByIds(ids));
    }

    @Autowired
    private IFieldStatusService fieldStatusService;

    @PostMapping("/selectStatus")
    @ResponseBody
    public AjaxResult selectStatus(String sceneModel) {
        FieldStatus fieldStatus = new FieldStatus();
        fieldStatus.setSceneModel(sceneModel);
        return AjaxResult.success(fieldStatusService.selectFieldStatusList(fieldStatus));
    }
}
